package org.pool.custom;

import org.apache.commons.dbcp.BasicDataSource;
import org.common.constant.GlobalConfig;
import org.common.util.JDBCUtil;

import java.util.Properties;

/**
 * 模拟一个连接池工厂
 *
 * @author yexi
 */
public class CustomDataSourceFactory {

    private CustomDataSourceFactory() {

    }

    private static class FactoryHolder {
        private final static CustomDataSourceFactory factory = new CustomDataSourceFactory();
    }

    private static class DataSourceHolder {
        private final static BasicDataSource dataSource = new BasicDataSource();
    }

    public static CustomDataSourceFactory getInstance() {
        return FactoryHolder.factory;
    }

    public BasicDataSource createDataSource() {
        BasicDataSource dataSource = DataSourceHolder.dataSource;
        String active = PROPERTIES.getProperty("active");
        dataSource.setDriverClassName(PROPERTIES.getProperty(GlobalConfig.getDriver(active)));
        dataSource.setUsername(PROPERTIES.getProperty(GlobalConfig.getUsername(active)));
        dataSource.setPassword(PROPERTIES.getProperty(GlobalConfig.getPassword(active)));
        dataSource.setUrl(PROPERTIES.getProperty(GlobalConfig.getUrl(active)));
        return dataSource;
    }

    private static final Properties PROPERTIES = JDBCUtil.getProperties();

}
